package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.entity.Goods
import com.zxy.supplier_system.server.service.CheckInventoryRecordService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("checkInventoryRecords/{id}/items")
class CheckInventoryRecordsItemController(
    private val checkInventoryRecordService: CheckInventoryRecordService
) {

    data class AddCheckInventoryRecordsItemRequest(
        val goodsId: Int,
        val amount: Int,
        val subGoodsId:Int? = null,
    )

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PostMapping
    fun addItemWithAmount(
        @PathVariable id: Int,
        @RequestBody addCheckInventoryRecordsItemRequest: AddCheckInventoryRecordsItemRequest
    ) {
        this.checkInventoryRecordService.addItem(id, addCheckInventoryRecordsItemRequest)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @DeleteMapping("{goodsId}")
    fun deleteItem(@PathVariable goodsId: Int, @PathVariable id: Int) {
        this.checkInventoryRecordService.deleteItem(id, goodsId)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @DeleteMapping("{goodsId}/sub/{subGoodsId}")
    fun deleteSubItem(@PathVariable goodsId: Int, @PathVariable id: Int,@PathVariable subGoodsId: Int) {
        this.checkInventoryRecordService.deleteItem(id, goodsId,subGoodsId)
    }

    @Transactional
    class UpdateItemAmountRequest(
        val amount: Int
    )

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PatchMapping("{goodsId}/sub/{subGoodsId}/amount")
    fun updateItemAmount(
        @PathVariable goodsId: Int,
        @PathVariable id: Int,
        @RequestBody updateItemAmountRequest: UpdateItemAmountRequest,
        @PathVariable subGoodsId: Int
    ) {
        return this.checkInventoryRecordService.updateItemAmount(goodsId, id, updateItemAmountRequest,subGoodsId)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PatchMapping("{goodsId}/amount")
    fun updateItemAmount(
        @PathVariable goodsId: Int,
        @PathVariable id: Int,
        @RequestBody updateItemAmountRequest: UpdateItemAmountRequest
    ) {
        return this.checkInventoryRecordService.updateItemAmount(goodsId, id, updateItemAmountRequest)
    }

    @HasAuthority(Authority.库存_盘点_查询)
    @GetMapping
    fun queryOrderItemsInOrder(@PathVariable id: Int, pageable: Pageable): Page<CheckInventoryRecordItemSummary> {
        return this.checkInventoryRecordService.queryOrderItemsInOrder(id, pageable)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @GetMapping("{goodsId}/exists")
    fun exists(@PathVariable goodsId: Int, @PathVariable id: Int): Boolean {
        return this.checkInventoryRecordService.exists(id, goodsId)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @GetMapping("barcodes/{barcode}/exists")
    fun exists(@PathVariable barcode: String, @PathVariable id: Int): Boolean {
        return this.checkInventoryRecordService.exists(id, barcode)
    }

    data class CheckInventoryRecordItemSummary(
        val goodsId: Int,
        val name: String,
        val barcode: String,
        val amount: Int,
        val inStore: Int,
        val boxSpecification: String?
    ) {
        companion object {
            fun fromGoodsAndAmount(goods: Goods, amount: Int): CheckInventoryRecordItemSummary {
                return CheckInventoryRecordItemSummary(
                    goods.id,
                    goods.name,
                    goods.barcode,
                    amount,
                    goods.amount,
                    goods.boxSpecification,
                )
            }
        }
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PostMapping("barcodes/{barcode}")
    fun addItem(@PathVariable barcode: String, @PathVariable id: Int): CheckInventoryRecordItemSummary {
        return this.checkInventoryRecordService.addItem(id, barcode)
    }

    @HasAnyAuthority(Authority.库存_盘点_盘点)
    @PostMapping("goods/{goodsId}")
    fun addItem(@PathVariable goodsId: Int, @PathVariable id: Int): CheckInventoryRecordItemSummary {
        return this.checkInventoryRecordService.addItem(id, goodsId)
    }

}